import { createWebHashHistory, createRouter } from "vue-router";
import VerticalLayout from '@/layout/VerticalLayout.vue'
import MenuLayout from '@/layout/MenuLayout.vue'

// 拿到全部业务路由
const ctx = import.meta.globEager("./components/*.js");
const routes = {};
for (const key in ctx) {
  const value = Object.values(ctx[key]);
  const segments =  key.split('/')
  const fileName = segments[segments.length - 1];
  const componentName = fileName.split('.')[0];
  routes[componentName] = []
  value.forEach((x) => {
    if (x instanceof Array) {
      x.forEach((r) => routes[componentName].push(r));
    } else if (x instanceof Object) {
      routes[componentName].push(x);
    }
  });
}


const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: "/Login",
      name: "Login",
      component: () => import("@/views/Login/index.vue"),
    },
    {
      path: "/",
      redirect: routes.MenuLayout[0].path,
      component: MenuLayout,
      meta: {
        title: "首页"
      },
      children: routes.MenuLayout
    },
    {
      path: "/vertical",
      redirect: routes.VerticalLayout[0].path,
      component: VerticalLayout,
      children: routes.VerticalLayout
    }
  ],
});

export default router;
